# @Version        : 1.0
# @Update Time    : 2025/3/14 13:43
# @File           : search_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息

import flet as ft


class SearchView(ft.Column):
    def __init__(self, page: ft.Page, params: dict = None):
        super().__init__()
        self.page = page
        self.params = params
        self.search_query = ft.TextField(
            hint_text="搜索新鲜食材",
            border_radius=20,
            bgcolor=ft.colors.GREEN_100,
            border_color=ft.colors.TRANSPARENT,
            prefix_icon=ft.icons.SEARCH,
            height=45,
            expand=True
        )
        self.build_interface()

    def build_interface(self):
        # 构建顶部搜索栏
        search_bar = ft.Row(
            controls=[
                ft.IconButton(ft.icons.ARROW_BACK, on_click=lambda e: self.return_home(), icon_color="grey"),
                self.search_query,
                ft.IconButton(
                    icon=ft.icons.SEARCH,
                    icon_color=ft.colors.GREEN_800,  # 设置搜索图标的颜色
                    on_click=lambda _: print("搜索按钮点击事件")
                )
            ],
            alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
            expand=True,
        )

        # 最近搜索记录
        recent_searches = ft.Column(
            [
                ft.Text("最近搜索", size=16, weight=ft.FontWeight.BOLD),
                ft.Row(
                    [
                        self.create_food_item("🥭", "芒果"),
                        self.create_food_item("🥑", "牛油果"),
                        self.create_food_item("🍞", "面包")
                    ], scroll=ft.ScrollMode.AUTO
                )
            ]
        )

        # 标签区域
        tags_list = ["新鲜", "有机", "本地", "健康", "无添加", "绿色", "非转基因"]  # 添加: 定义 tags_list
        tags = self.create_tags(tags_list)
        # 热门推荐
        hot_items = ft.Column(
            [
                ft.Text("热门", size=16, weight=ft.FontWeight.BOLD),
                ft.Row(
                    [
                        self.create_product_card("🥭", "芒果", "16.8kg"),
                        self.create_product_card("🍇", "葡萄", "8.6kg")
                    ],
                    spacing=20
                )
            ], spacing=15
        )
        # 组合所有组件
        self.controls = [
            search_bar,
            recent_searches,
            tags,
            hot_items
        ]
        self.spacing = 20
        self.expand = True  # 确保 SearchView 可以扩展以适应页面高度

    def create_food_item(self, emoji, name):
        return ft.Column(
            [
                ft.Container(
                    content=ft.Text(emoji, size=36),
                    width=80,
                    height=80,
                    bgcolor=ft.colors.GREY_100,
                    border_radius=10,
                    alignment=ft.alignment.center
                ),
                ft.Text(name, size=12)
            ], spacing=5, width=100
        )

    def create_tags(self, tags_list):
        def on_resize(e):
            container_width = self.page.window.width
            tag_width = 100  # 假设每个标签的宽度为100像素
            max_tags_per_row = int(container_width // tag_width)
            tags = ft.Column(
                [
                    ft.Text("标签", size=16, weight=ft.FontWeight.BOLD),
                    ft.Column(
                        [
                            ft.Row(
                                [
                                    ft.Container(
                                        content=ft.Text(tag, size=14),
                                        padding=10,
                                        border_radius=20,
                                        border=ft.border.all(1, ft.colors.GREY_300)
                                    ) for tag in tags_chunk
                                ],
                                spacing=10
                            ) for tags_chunk in
                            [tags_list[i:i + max_tags_per_row] for i in range(0, len(tags_list), max_tags_per_row)]
                        ], spacing=10
                    )
                ]
            )
            self.controls[2] = tags  # 更新标签区域
            self.update()

        # 初始创建标签
        container_width = self.page.window.width
        tag_width = 100  # 假设每个标签的宽度为100像素
        max_tags_per_row = int(container_width // tag_width)
        tags = ft.Column(
            [
                ft.Text("标签", size=16, weight=ft.FontWeight.BOLD),
                ft.Column(
                    [
                        ft.Row(
                            [
                                ft.Container(
                                    content=ft.Text(tag, size=14),
                                    padding=10,
                                    border_radius=20,
                                    border=ft.border.all(1, ft.colors.GREY_300)
                                ) for tag in tags_chunk
                            ],
                            spacing=10
                        ) for tags_chunk in
                        [tags_list[i:i + max_tags_per_row] for i in range(0, len(tags_list), max_tags_per_row)]
                    ], spacing=10
                )
            ]
        )

        # 添加窗口大小改变事件监听
        self.page.on_resize = on_resize

        return tags

    def create_product_card(self, emoji, name, weight):
        return ft.Container(
            content=ft.Column(
                [
                    ft.Text(emoji, size=36),
                    ft.Text(name, size=14, weight=ft.FontWeight.BOLD),
                    ft.Row(
                        [
                            ft.Text(weight, size=12, color=ft.colors.GREY_600),
                            ft.IconButton(ft.icons.ADD, icon_size=20)
                        ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN
                    )
                ], spacing=10
            ),
            width=150,
            padding=15,
            bgcolor=ft.colors.GREY_100,
            border_radius=10
        )

    def add_to_input(self, char):
        self.search_query.value += char
        self.search_query.update()

    def clear_search(self):
        self.search_query.value = ""
        self.search_query.update()

    def return_home(self):
        self.page.views.pop()
        self.page.update()
